package com.example.cloud_disk.FileReading;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 用于读取Excel文件的服务类
 */
@Service
public class ExcelService {

    public Map<String, String> readExcel(MultipartFile excelFile) {
        Map<String, String> studentMap = new HashMap<>();

        try (InputStream inputStream = excelFile.getInputStream();
             Workbook workbook = new XSSFWorkbook(inputStream)) {

            Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表

            // 读取第一行，获取列名
            Row headerRow = sheet.getRow(0);
            int nameColumnIndex = -1;
            int studentIdColumnIndex = -1;

            for (int i = 0; i < headerRow.getLastCellNum(); i++) {
                Cell cell = headerRow.getCell(i);
                if (cell != null) {
                    String columnName = getCellValueAsString(cell);
                    if ("姓名".equals(columnName)) {
                        nameColumnIndex = i;
                    } else if ("学号".equals(columnName)) {
                        studentIdColumnIndex = i;
                    }
                }
            }

            if (nameColumnIndex == -1 || studentIdColumnIndex == -1) {
                System.out.println("Excel文件中缺少必要的列");
                return studentMap;
            }

            // 从第二行开始读取数据
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                Cell nameCell = row.getCell(nameColumnIndex);
                Cell studentIdCell = row.getCell(studentIdColumnIndex);

                if (nameCell != null && studentIdCell != null) {
                    String name = getCellValueAsString(nameCell).trim();
                    String studentId = getCellValueAsString(studentIdCell).trim();
                    studentMap.put(name, studentId);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return studentMap;
    }

    private String getCellValueAsString(Cell cell) {
        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue();
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    return cell.getDateCellValue().toString();
                } else {
                    // 避免科学记数法，将数值转换为文本格式
                    cell.setCellType(CellType.STRING);
                    return cell.getStringCellValue();
                }
            case BOOLEAN:
                return Boolean.toString(cell.getBooleanCellValue());
            case FORMULA:
                try {
                    // 如果单元格是公式类型，获取其结果
                    return getCellValueAsString(cell);
                } catch (Exception e) {
                    // 如果公式计算失败，返回空字符串
                    return "";
                }
            default:
                return "";
        }
    }
}